/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Clases;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author JULIAN
 */
public class ListarEmpleados extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();

         HttpSession session_actual = request.getSession(true);
         String almacen = (String) session_actual.getAttribute("alma");
         PreparedStatement st = null;
         PreparedStatement st2 = null;
         SQL_Consultas obj = new SQL_Consultas();
         obj.setAlmacen(almacen);
         st=obj.GetJPEMP();
       
        try {
        
        ResultSet resultado = st.executeQuery();
        
                out.println("<html>");
                out.println("<head>");
                out.println("<title> :::RESULTADO DE CONSULTA::: </title>");
                out.println("</head>");
                out.println("<body bgcolor='#000000'>");                
                out.println("<div style=\'text-align:center\'>");
                out.println("<h2>PERSONAL</h2>");
                out.println("<form method=post action=EditarHorasExtra>");
                out.println("<td colspan=2>");
                out.println("<table align = center border = 1>");                
                out.println("<thread>");
                out.println("<th><font color=white>Codigo</th></font>");
                out.println("<th><font color=white>Nombre</th></font>");
                out.println("<th><font color=white>Apellido</th></font>");
                out.println("<th><font color=white>Fecha inicio</th></font>");
                out.println("<th><font color=white>Tipo de Contrato</th></font>");
                out.println("<th><font color=white>Salario</th></font>");
                out.println("<th><font color=white>cargo</th></font>");
                out.println("<th><font color=white>Almacen</th></font>");
                out.println("<th><font color=white>Turno laboral</th></font>");
                out.println("<th><font color=white>horas extas mes actual</th></font>");
                out.println("</td>");
                out.println("</thread>");
                out.println("</tbody>");

                while(resultado.next()){
                obj.setId(Integer.toString(resultado.getInt("id_empleado")));
                st2=obj.GetExtrasEMP();
                ResultSet resultado2 = st2.executeQuery();
                

                out.println("<tr>");
                out.println("<td><font color=white>"+resultado.getInt("id_empleado")+"</td></font>");
                out.println("<td><font color=white>"+resultado.getString("nombres")+"</td></font>");
                out.println("<td><font color=white>"+resultado.getString("apellidos")+"</td></font>");
                out.println("<td><font color=white>"+resultado.getDate("fecha_inicio")+"</td></font>");
                out.println("<td><font color=white>"+resultado.getString("tipo_contrato")+"</td></font>");
                out.println("<td><font color=white>"+resultado.getDouble("salario")+"</td></font>");
                out.println("<td><font color=white>"+resultado.getString("nombre_cargo")+"</td></font>");
                out.println("<td><font color=white>"+resultado.getInt("id_almacen")+"</td></font>");
                out.println("<td><font color=white>"+resultado.getString("nombre_turno")+"</td></font>");
               if(!resultado2.next()){
                out.println("<td><font color=white>0</td>");
                                     }
               else{
                out.println("<td><font color=white>"+resultado2.getInt("horas_extra")+"</td></font>");
                   }
                out.println("</tr>");
                resultado2.close();
                st2.close();
                                  }
                out.println("</tbody>");
                out.println("</table>");
                out.println("<a href = \"opcionesJP.jsp\"> << Regresar </a>");
                out.println("</div>");
                out.println("</body>");
                out.println("</html>");

                st.close();
              
        } catch (SQLException ex) {
            Logger.getLogger(ListarEmpleados.class.getName()).log(Level.SEVERE, null, ex);
        }

    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ListarEmpleados.class.getName()).log(Level.SEVERE, null, ex);
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (SQLException ex) {
            Logger.getLogger(ListarEmpleados.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
